<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\UsersModel;
use mbilling\BillingBundle\Model\OrdersVpsTarifModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\ordersVpsTarifs;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\OrdersVpsTarifForm;

class OrdersVpsTarifsController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarif_model = new OrdersVpsTarifModel($em);
        $tarifs = $tarif_model->findAll();
        $has_orders = array();
        foreach($tarifs as $tarif)
        {
            if ($tarif_model->checkOrders($tarif->getId()))
                $has_orders[$tarif->getId()] = '1';
            else
                $has_orders[$tarif->getId()] = '0';
        }
        return $this->render('BillingBundle:Orders_vps_tarifs:index.html.twig', array('tarifs' => $tarifs, 'has_orders' => $has_orders));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarif_model = new OrdersVpsTarifModel($em);

        $tarif = new ordersVpsTarifs();
        //$tarif_model->create($tarif->initDefaultEntity());//создание по умолчанию
        //return new RedirectResponse($this->generateUrl('orders_vps_tarifs_show'));
        $tarif->setDedIpCount(2);
        $form = $this->createForm(new OrdersVpsTarifForm, $tarif);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $tarif_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('7');//страница тарифы на VPS
                $log->setObjectId($tarif->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание тарифа
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('orders_vps_tarifs_show'));
            }
        }
        return $this->render('BillingBundle:Orders_vps_tarifs:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarif_model = new OrdersVpsTarifModel($em);

        $tarif = $tarif_model->findByPK($id);

        $form = $this->createForm(new OrdersVpsTarifForm, $tarif);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $tarif_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('7');//страница тарифы на VPS
                $log->setObjectId($tarif->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение тарифа
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('orders_vps_tarifs_show'));
            }
        }
        return $this->render('BillingBundle:Orders_vps_tarifs:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $tarif_model = new OrdersVpsTarifModel($em);
        
        $tarif = $tarif_model->findByPK($id);
        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();
        
        $tarif_model->removeByPK($id);
        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('7');//страница тарифы на VPS
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление тарифа
        $log_model->create($log);

        return new RedirectResponse($this->generateUrl('orders_vps_tarifs_show'));
    }
}